package com.businesswebsite.businesswebsite.FormalWebExplot.Controller;
import com.alibaba.fastjson.JSONObject;
import com.businesswebsite.businesswebsite.FormalWebExplot.Entity.Product;
import com.businesswebsite.businesswebsite.FormalWebExplot.Service.Product.ProductService;
import com.businesswebsite.businesswebsite.FormalWebExplot.Service.Utils.FileUploadTool;
import com.businesswebsite.businesswebsite.FormalWebExplot.Utils.AliOSSUtils;
import com.businesswebsite.businesswebsite.FormalWebExplot.Utils.Renum;
import com.businesswebsite.businesswebsite.FormalWebExplot.Utils.Result;
import com.businesswebsite.businesswebsite.FormalWebExplot.Utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@CrossOrigin(origins = "http://47.108.80.35:8080" , allowCredentials = "true")
//@CrossOrigin(origins = "http://localhost:8080" , allowCredentials = "true")
@RestController
@RequestMapping("/company_req_product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private FileUploadTool fileUploadTool;
    @Autowired
    private AliOSSUtils aliOSSUtils;
    @PostMapping("/add_product")
    public Result addProduct(@RequestParam Map<String,Object> product,@RequestParam("file") MultipartFile file){
        String id = UUID.randomUUID().toString().replace("-","");
        product.put("pro_id",id);
        try {
            /**
            //上传文件 初次上传 必选图片
            if(file.getOriginalFilename()!=null){
                fileUploadTool.fileUpLoad(file,id);
                //获取文件后缀 完成拼接
                String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
                product.put("pro_filename",id+"."+fileType);
            }else{
                return ResultUtil.error(Renum.DATA_IS_NULL.getCode(),"请上传图片");
            }
            */
            String ImgUrl = aliOSSUtils.upload(file);
            if(ImgUrl == null){
                return ResultUtil.error(Renum.DATA_IS_NULL.getCode(),"图片上传失败");
            }
            product.put("pro_filename",ImgUrl);
            if(product.get("pro_name")!=null && product.get("pro_detail")!=null){
                Integer addProduct = productService.addProduct(product);

                if(addProduct > 0 ){
                    return ResultUtil.success("添加成功"+"\n\n"+ JSONObject.toJSON(product));
                }
                return ResultUtil.error(Renum.UNKNOWN_ERROR.getCode(),Renum.UNKNOWN_ERROR.getMsg());
            }
            return ResultUtil.error(Renum.DATA_IS_NULL.getCode(),Renum.DATA_IS_NULL.getMsg());
        }catch (Exception e) {
            return  ResultUtil.error(Renum.ERROR.getCode(),e.getMessage());
        }
    }
    //文件上传
    @PostMapping("/upload_img")
    public Result uploadImg(@RequestParam("file") MultipartFile file){
        Result<String> stringResult = fileUploadTool.fileUpLoad(file, UUID.randomUUID().toString().replace("-", ""));
        return stringResult;
    }

    //    阿里云文件上传OSS
    @PostMapping("/upload_oss_img")
    public Result uploadOssImg(@RequestParam("file") MultipartFile file) throws IOException {
        String url = aliOSSUtils.upload(file);
        return ResultUtil.success(url);
    }

    // 获取产品
    @GetMapping("/get_product")
    public Result getProduct(){
        try{
            List<Product> product = productService.getProduct();
            if(product == null){
                return ResultUtil.error(Renum.DATA_IS_NULL.getCode(), "获取产品列表失败");
            }
            return ResultUtil.success(JSONObject.toJSON(product));
        }catch (RuntimeException e){
            e.printStackTrace();
            return ResultUtil.error(Renum.UNKNOWN_ERROR.getCode(), e.getMessage());
        }
    }
    //删除对应id产品
    @PostMapping("/del_product")
    public Result delProductByid(@RequestParam("id") String pro_id){
        try{
            if(pro_id ==null){
                return ResultUtil.error(Renum.DATA_IS_NULL.getCode(), "产品id不能为空");
            }
            Integer delPro = productService.delProduct(pro_id);
            if(delPro > 0 ){
                return ResultUtil.success("删除成功");
            }
            return ResultUtil.error(Renum.UNKNOWN_ERROR.getCode(),"删除失败,请确认对应产品是否存在");
        }catch (RuntimeException e){
            e.printStackTrace();
            return ResultUtil.error(Renum.UNKNOWN_ERROR.getCode(), e.getMessage());
        }
    }
}
